package com.bjsxt.config.jackson;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.text.SimpleDateFormat;
import java.util.TimeZone;
//Jackson是一个流行的Java库，用于处理JSON数据。通过配置ObjectMapper，可以自定义JSON序列化和反序列化的行为。
@Configuration
public class JacksonConfig {

    // 注册ObjectMapper Bean
    @Bean
    public ObjectMapper objectMapper() {
        // 创建ObjectMapper对象
        ObjectMapper objectMapper = new ObjectMapper();
        // 注册JavaTimeModule模块，用于处理Java 8的时间类
        objectMapper.registerModule(new JavaTimeModule());
        // 创建SimpleModule对象
        SimpleModule simpleModule = new SimpleModule();
        // 添加Long类的序列化器，将Long类型转换为字符串
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        // 注册SimpleModule模块
        objectMapper.registerModule(simpleModule);
        // 设置时区为Asia/Shanghai
        objectMapper.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        // 设置日期格式为yyyy-MM-dd HH:mm:ss
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        // 设置所有属性的可见性为ANY
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);

        return objectMapper;
    }
}
